﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnWebsite.Models;
using Ext.Net;
using Ext.Net.MVC;
using System.Linq.Dynamic;

namespace DoAnWebsite.Controllers
{
    public class DocumentTypeController : Controller
    {
        //News
        private static DoAnWebsite_Entities db = new DoAnWebsite_Entities();
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult ListDocumentType()
        {
            Session["includeInActiveDocumentType"] = false;
            Session["CharFilterDocumentType"] = "";
            return View();
        }

        public ActionResult IncludeInActive()
        {
            var store = X.GetCmp<Store>("DocumentTypeStore");

            Session["includeInActiveDocumentType"] = !(bool)Session["includeInActiveDocumentType"];

            store.Reload();
            return this.Direct();
        }

        [DirectMethod]
        public ActionResult CharFilterDocumentType(string filterstring)
        {
            var store = X.GetCmp<Store>("DocumentTypeStore");
            Session["CharFilterDocumentType"] = filterstring;
            store.Reload();
            return this.Direct();
        }

        public ActionResult BindingDocumentTypeStore(StoreRequestParameters parameters)
        {
            var sampleData = db.DocumentTypes.Select(m => new
            {
                m.TypeId,
                m.DTName,
                m.Image,
                m.Active
            });


            bool includeInActive = (bool)Session["includeInActiveDocumentType"];
            string charFilter = (string)Session["CharFilterDocumentType"];

            var result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active)));
            if ("0".Equals(charFilter))
            {
                result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active) &&
                    (ent.DTName.StartsWith("0") || ent.DTName.StartsWith("1") ||
                    ent.DTName.StartsWith("2") || ent.DTName.StartsWith("3") ||
                    ent.DTName.StartsWith("4") || ent.DTName.StartsWith("5") ||
                    ent.DTName.StartsWith("6") || ent.DTName.StartsWith("7") ||
                    ent.DTName.StartsWith("8") || ent.DTName.StartsWith("9")
                    )));
            }

            else
            {
                result = sampleData.Where(ent => ((includeInActive || ent.Active) && ent.DTName.StartsWith(charFilter)));

            }

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result = result.OrderBy(parameters.SimpleSort + (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderBy(ent => ent.TypeId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData.Select(ent => new { ent.TypeId, ent.DTName, ent.Image, ent.Active }), result.Count());
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult CreateDocumentType()
        {
            ViewBag.HideTextBox = false;

            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateDocumentType(DocumentType model)
        {
            if (ModelState.IsValid)
            {
                model.Active = true;
                db.DocumentTypes.AddObject(model);
                db.SaveChanges();
                X.Msg.Notify("Sucess", "Create DocumentType success!").Show();
                return this.FormPanel();
            }

            X.Msg.Notify("Non Sucess", "Create DocumentType non success!").Show();

            return this.FormPanel();
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult EditDocumentType(int id)
        {
            ViewBag.HideTextBox = false;
            DocumentType documentType = db.DocumentTypes.Single(m => m.TypeId == id);

            return View(documentType);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDocumentType(DocumentType model)
        {
            DocumentType documentType1 = db.DocumentTypes.Single(c => c.TypeId == model.TypeId);
            if (ModelState.IsValid)
            {
                db.DocumentTypes.Detach(documentType1);
                db.DocumentTypes.Attach(model);
                db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                db.SaveChanges();
                X.Msg.Notify("Sucess", "Edit DocumentType success!").Show();
                return this.FormPanel();
            }
            X.Msg.Notify("Non Sucess", "Edit DocumentType non success!").Show();
            return this.FormPanel();
        }

    }
}
